<?php

declare(strict_types=1);

namespace App\Exchange\Listener;

use App\Fastapp\Listener\Event\AccountRegisterEvent;
use App\Fastapp\Model\AccountWallet;
use App\Fastapp\Model\Currency;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Psr\Container\ContainerInterface;
use Swoole\Coroutine;

#[Listener]
class ExchangeListener implements ListenerInterface
{
    public function __construct(protected ContainerInterface $container)
    {
    }

    public function listen(): array
    {
        return [
            AccountRegisterEvent::class,
        ];
    }

    public function process(object $event): void
    {
        Coroutine::create(function () use ($event) {
            if ($event instanceof AccountRegisterEvent && str_starts_with((string)$event->account_type, '8')) {
                $ids = Currency::query()->where(['status' => 1])->pluck('symbol', 'id');
                $in = [];
                foreach ($ids as $id => $symbol) {
                    $in[] = [
                        'currency_id' => $id,
                        'user_id' => $event->user_id,
                        'symbol' => $symbol,
                    ];
                }
                AccountWallet::query()->insert($in);
            }
        });
    }
}
